//package com.plian.system.service.dj.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.config.YthTaskObserver;
//import com.plian.system.constant.baseinfo.ValueSetConstant;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.dao.pm.property.PropertyBasicsDao;
//import com.plian.system.entity.dj.DjPartyLecturerInfo;
//import com.plian.system.entity.sys.Org;
//import com.plian.system.entity.sys.User;
//import com.plian.system.mapper.dj.DjPartyLecturerEvaluationMapper;
//import com.plian.system.model.R;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.bt.baseinfo.BaseInfoService;
//import com.plian.system.service.dj.IDjPartyLecturerInfoService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.sys.orgzation.IOrgService;
//import com.plian.system.service.sys.user.ISysUserService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.service.yth.IPartyManagementService;
//import com.plian.system.vo.dj.DjPartyLecturerInfoVO;
//import com.plian.system.wrapper.dj.DjPartyLecturerInfoWrapper;
//import lombok.AllArgsConstructor;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 党建讲师库管理
// **/
//@Slf4j
//@Service
//@AllArgsConstructor
//public class DjPartyLecturerInfoServiceImpl extends BaseServiceImpl<DjPartyLecturerEvaluationMapper, DjPartyLecturerInfo> implements IDjPartyLecturerInfoService, BaseFlowService {
//
//
//    private FormWorkflowService formWorkflowService;
//
//    private IFormCodeService formCodeService;
//
//    private final DjPartyLecturerInfoWrapper partyLecturerEvaluationWrapper;
//
//    private ISysUserService userService;
//
//    private final IOrgService orgService;
//
//    private BaseInfoService baseInfoService;
//
//    @Autowired
//    private PropertyBasicsDao propertyBasicsDao;
//
//    private IPartyManagementService partyManagementService;
//
//
//    @Override
//    @Transactional(rollbackFor = Throwable.class)
//    public boolean submit(DjPartyLecturerInfo partyLecturerEvaluation) {
//        if (!Optional.ofNullable(partyLecturerEvaluation.getStatus()).isPresent()) {
//            partyLecturerEvaluation.setStatus(FormStatusConstant.UNREVIEWED);
//        }
//        return saveOrUpdate(partyLecturerEvaluation);
//    }
//
//    @SneakyThrows
//    @Override
//    public boolean saveOrUpdate(DjPartyLecturerInfo partyLecturerEvaluation) {
//        boolean flag = false;
//        User user = userService.getById(TokenUtil.getTokenUserId());
//        if (partyLecturerEvaluation.getId() == null) {
//            flag = true;
//            partyLecturerEvaluation.setCode(formCodeService.getCode(FormTypeConstant.DJ_PARTYLECTURER_EVALUATION));
//            partyLecturerEvaluation.setFormType(String.valueOf(FormTypeConstant.DJ_PARTYLECTURER_EVALUATION));
//            if (StringUtil.isBlank(partyLecturerEvaluation.getFillingUnitId())) {
//                partyLecturerEvaluation.setFillingUnitId(TokenUtil.getTokenOrgId());
//            }
//            if (StringUtil.isBlank(partyLecturerEvaluation.getFillingUnitName())) {
//                partyLecturerEvaluation.setFillingUnitName(TokenUtil.getCompetentOrganization());
//            }
//            partyLecturerEvaluation.setFillingTime(new Date());
//            partyLecturerEvaluation.setFillingCreateUserCode(TokenUtil.getTokenUserId());
//            partyLecturerEvaluation.setFillingCreateUserName(user.getUsername());
//        }
//        if (StringUtil.isNotBlank(partyLecturerEvaluation.getSpeakerUnitId())) {
//            PageData data = propertyBasicsDao.findByorgId(partyLecturerEvaluation.getSpeakerUnitId());
//            partyLecturerEvaluation.setSpeakerUnitName(data.get("stateFundedName").toString());
//        }
//        boolean result = super.saveOrUpdate(partyLecturerEvaluation);
//        return result;
//    }
//
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            DjPartyLecturerInfo partyLecturerEvaluation = getById(id);
//            if (partyLecturerEvaluation == null) {
//                return false;
//            } else if (partyLecturerEvaluation.getStatus() != null &&
//                    (partyLecturerEvaluation.getStatus() != FormStatusConstant.STASH && partyLecturerEvaluation.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//            formWorkflowService.deleteProcessInstance(String.valueOf(id));
//        }
//        boolean result = super.deleteLogic(ids);
//        return result;
//    }
//
//    @Override
//    public HashMap<String, Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String, Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        /**
//         * 设置审批人和审批时间
//         */
//        DjPartyLecturerInfo partyLecturerEvaluation = getById(id);
//        partyLecturerEvaluation.setApprovedUser(TokenUtil.getTokenUserId());
//        Date now = DateUtil.now();
//        partyLecturerEvaluation.setApprovedTime(now);
//        updateById(partyLecturerEvaluation);
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        List<DjPartyLecturerInfo> djPartymemberBaseinfos = list(new LambdaQueryWrapper<DjPartyLecturerInfo>()
//                .in(DjPartyLecturerInfo::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA))));
//        List<DjPartyLecturerInfoVO> djPartymemberBaseinfoVOList = partyLecturerEvaluationWrapper.entityToVO(djPartymemberBaseinfos);
//        List<PageData> pageDataList = new ArrayList<>();
//        Org org = orgService.getById(TokenUtil.getTokenOrgId());
//        try {
//            if (CollectionUtil.isNotEmpty(djPartymemberBaseinfoVOList)) {
//                for (DjPartyLecturerInfoVO vo : djPartymemberBaseinfoVOList) {
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", org.getName());
//                    pd.put("APPLICANTNAME", vo.getCreateUser());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", org.getId());
//                    pd.put("formType", FormTypeConstant.DJ_PARTYLECTURER_EVALUATION);
//                    pd.put("orgName", org.getName());
//                    pageDataList.add(pd);
//                }
//            }
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(FormTypeConstant.DJ_PARTYLECTURER_EVALUATION);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     *
//     * @param id           主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            DjPartyLecturerInfo partyLecturerEvaluation = getById(id);
//            partyLecturerEvaluation.setStatus(updateStatus);
//            updateById(partyLecturerEvaluation);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//    @Override
//    public R analyseLecturerEvaluation(DjPartyLecturerInfoVO partyLecturerInfoVO, String flag) {
//        List<Map<String, Object>> result = new ArrayList<>();
//        Map<String, Object> data = new HashMap<>();
//        try {
//            if ("tag".equals(flag)) {
//                List<DjPartyLecturerInfo> partyLecturerInfoList = baseMapper.selectList(new LambdaQueryWrapper<DjPartyLecturerInfo>()
//                        .eq(DjPartyLecturerInfo::getSpeakerUnitId, partyLecturerInfoVO.getSpeakerUnitId()));
//                data = getAnalyseDataTag(partyLecturerInfoList);
//            } else {
//                List<DjPartyLecturerInfo> partyLecturerInfoList = baseMapper.selectList(new LambdaQueryWrapper<DjPartyLecturerInfo>()
//                        .eq(DjPartyLecturerInfo::getFillingUnitId, "2c26d169d65347b064ba645a487257e5"));
//                data = getAnalyseDataUnit(partyLecturerInfoList);
//            }
//            result.add(data);
//        } catch (Exception e) {
//            log.error(e.getMessage());
//        }
//        return R.data(result);
//    }
//
//    /**
//     * @param partyLecturerInfoList
//     * @return com.plian.Tools.pagedata.PageData
//     **/
//    private Map<String, Object> getAnalyseDataTag(List<DjPartyLecturerInfo> partyLecturerInfoList) {
//        Map<String, Object> map = new HashMap<>();
//        Map<String, String> childMap = baseInfoService.getChildMap(ValueSetConstant.DJ_PARTY_TAGS);
//
//        for (String key : childMap.keySet()) {
//            map.put(key, 0L);
//        }
//        for (DjPartyLecturerInfo vo : partyLecturerInfoList) {
//            String speakerTag = vo.getSpeakerTag();
//            if (StringUtil.isNotBlank(speakerTag)) {
//                if (speakerTag.contains(",")) {
//                    //多个
//                    String[] tagIds = speakerTag.split(",");
//                    for (String tag : tagIds) {
//                        Object value = map.get(tag);
//                        if (value != null) {
//                            long count = Long.parseLong(value.toString());
//                            map.put(tag, ++count);
//                        }
//                    }
//                } else {
//                    //单个
//                    Object value = map.get(speakerTag);
//                    if (value != null) {
//                        long count = Long.parseLong(value.toString());
//                        map.put(speakerTag, ++count);
//                    }
//                }
//            }
//        }
//        //3 转中文
//        Map<String, Object> result = new LinkedHashMap<>();
//        for (String key : map.keySet()) {
//            String tagName = childMap.get(key);
//            result.put(tagName, map.get(key));
//        }
//        return result;
//    }
//
//    /**
//     * @param partyLecturerInfoList
//     * @return java.util.Map<java.lang.String, java.lang.Object>
//     **/
//    private Map<String, Object> getAnalyseDataUnit(List<DjPartyLecturerInfo> partyLecturerInfoList) {
//        Map<String, Object> map = new HashMap<>();
//        for (DjPartyLecturerInfo vo : partyLecturerInfoList) {
//            String fillingUnitName = vo.getFillingUnitName();
//            Object value = map.get(fillingUnitName);
//            if (value == null) {
//                map.put(fillingUnitName, 1);
//            } else {
//                int count = Integer.parseInt(value.toString());
//                map.put(fillingUnitName, ++count);
//            }
//        }
//        return map;
//
//    }
//
//    @Override
//    @YthTaskObserver
//    public boolean saveDjPartyLecturerInfoByDataSource(String date) {
//        boolean flag = true;
//        Date start = DateUtil.parse(date, DateUtil.PATTERN_DATE);
//        //获取数据
//       // List<DjPartyLecturerInfo> syncDatas = partyManagementService.getSyncData(start, DateUtil.plusDays(start, 1));
//        List<DjPartyLecturerInfo> syncDatas = partyManagementService.getSyncData(null, null);
//
//        Map<String, String> orgSourceIds = orgService.getSourceId();
//        Map<String, String> userSourceIds = userService.getSourceId();
//
//        if (CollectionUtil.isNotEmpty(syncDatas)) {
//
//            //查询数据库存在的旧数据
//            List<DjPartyLecturerInfo> allOldData = list(new LambdaQueryWrapper<DjPartyLecturerInfo>()
//                    .eq(DjPartyLecturerInfo::getDataSource, 3)
//                    .in(DjPartyLecturerInfo::getDataSourceId, syncDatas.stream().map(DjPartyLecturerInfo::getDataSourceId).collect(Collectors.toList())));
//
//            for (DjPartyLecturerInfo syncData : syncDatas) {
//                //企业
//                String speakerUnitId = orgSourceIds.get(syncData.getSpeakerUnitId());
//                String fillingUnitId = orgSourceIds.get(syncData.getFillingUnitId());
//                String fillingCreateUserCode = userSourceIds.get(syncData.getFillingCreateUserCode());
//                if (StringUtils.isNotBlank(speakerUnitId)) {
//                    syncData.setSpeakerUnitId(speakerUnitId);
//                }
//                if (StringUtil.isNotBlank(fillingUnitId)) {
//                    syncData.setFillingUnitId(fillingUnitId);
//                    syncData.setCreateCompanyId(fillingUnitId);
//                }
//                if (StringUtil.isNotBlank(fillingCreateUserCode)) {
//                    syncData.setFillingCreateUserCode(fillingCreateUserCode);
//                }
//
//                if (CollectionUtil.isNotEmpty(allOldData)) {
//                    Optional<DjPartyLecturerInfo> oldDjPartyLecturerInfoOptional = allOldData.stream().filter(e -> syncData.getDataSourceId().equals(e.getDataSourceId())).findFirst();
//                    if (oldDjPartyLecturerInfoOptional.isPresent()) {
//                        DjPartyLecturerInfo partyLecturerInfo = oldDjPartyLecturerInfoOptional.get();
//                        partyLecturerInfo.setSpeakerName(syncData.getSpeakerName());
//                        partyLecturerInfo.setSpeakerEducationBk(syncData.getSpeakerEducationBk());
//                        partyLecturerInfo.setSpeakerRank(syncData.getSpeakerRank());
//                        partyLecturerInfo.setSpeakerUnitId(syncData.getSpeakerUnitId());
//                        partyLecturerInfo.setSpeakerUnitName(syncData.getSpeakerUnitName());
//                        partyLecturerInfo.setSpeakerPartyStanding(syncData.getSpeakerPartyStanding());
//                        partyLecturerInfo.setSpeakerTag(syncData.getSpeakerTag());
//                        partyLecturerInfo.setFillingUnitId(syncData.getFillingUnitId());
//                        partyLecturerInfo.setFillingUnitName(syncData.getFillingUnitName());
//                        partyLecturerInfo.setFillingCreateUserCode(syncData.getFillingCreateUserCode());
//                        partyLecturerInfo.setFillingCreateUserName(syncData.getFillingCreateUserName());
//                        partyLecturerInfo.setFillingTime(syncData.getFillingTime());
//                        flag = updateById(partyLecturerInfo);
//                    } else {
//                        flag = super.saveOrUpdate(syncData);
//                    }
//                } else {
//                    flag = super.saveOrUpdate(syncData);
//                }
//            }
//        }
//        return flag;
//    }
//}
